#include "slInput.hh"

slInput::slInput() : _x(0), _y(0), _xRel(0), _yRel(0), _finished(false)
{
	for (int i = 0; i < SDL_NUM_SCANCODES; i++)
		_keys[i] = false;

	for (int i = 0; i < 8; i++)
		_mouseButtons[i] = false;
}

void slInput::updateInput()
{
	_xRel = 0;
	_yRel = 0;

	while (SDL_PollEvent(&_event) == 1)
	{
		switch (_event.type)
		{
			case SDL_KEYDOWN:
				_keys[_event.key.keysym.scancode] = true;
			break;

			case SDL_KEYUP:
				_keys[_event.key.keysym.scancode] = false;
			break;

			case SDL_MOUSEBUTTONDOWN:
				_mouseButtons[_event.button.button] = true;
			break;

			case SDL_MOUSEBUTTONUP:
				_mouseButtons[_event.button.button] = false;
			break;			

			case SDL_MOUSEMOTION:
				_x = _event.motion.x;
				_y = _event.motion.y;
				_xRel = _event.motion.xrel;
				_yRel = _event.motion.yrel;
			break;

			if (_event.window.event == SDL_WINDOWEVENT_CLOSE)
				_finished = true;

			default:
			break;
		}
	}
}

int slInput::getX() const
{
	return _x;
}

int slInput::getY() const
{
	return _y;
}

int slInput::getXRel() const
{
	return _xRel;
}

int slInput::getYRel() const
{
	return _yRel;
}

void slInput::showCursor(bool state) const
{
	if (state)
		SDL_ShowCursor(SDL_ENABLE);
	else
		SDL_ShowCursor(SDL_DISABLE);
}

void slInput::catchCursor(bool state) const
{
	if (state)
		SDL_SetRelativeMouseMode(SDL_TRUE);
	else
		SDL_SetRelativeMouseMode(SDL_FALSE);
}

bool slInput::getMouseButton(const Uint8 button) const
{
	return _mouseButtons[button];
}

bool slInput::mouseMoved() const
{
	if (_xRel != 0 || _yRel != 0)
		return true;
	else
		return false;
}

bool slInput::getKey(const SDL_Scancode key) const
{
	return _keys[key];
}

bool slInput::isFinished() const
{
	return _finished;
}

slInput::~slInput()
{

}